import { addRequest, addWs } from '../requestManager.js';

addRequest({ method: 'post', path: '/hero' }, function ({ body }) {
  return { method: 'post', body };
});

addRequest({ method: 'put', path: '/hero/{id}' }, function ({ body, pathVars }) {
  return { method: 'put', pathId: pathVars.id, body };
});

addRequest({ method: 'get', path: '/hero' }, function ({ query }) {
  return { method: 'get', query };
});

addWs('/ws/client', function (ws, req) {
  console.log('【ws query】', req.query, req.path);
  ws.on('message', (msg) => {
    console.log('receive msg:', msg);
  });
  const timerId = setInterval(() => {
    ws.send('interval message' + Date.now());
  }, 5000);
  ws.on('close', () => {
    clearInterval(timerId);
    console.log(`closed(id=${req.query.id})`);
  });
});
